package com.xlh.fgs;

import java.util.ArrayList;
import java.util.HashMap;

import com.xlh.Utils.Constant;
import com.xlh.Utils.FUti;
import com.xlh.outside.R;

import android.app.Fragment;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class ESA_003Fg extends Fragment{


	private int containerWidth;


	public ESA_003Fg() {
		// TODO Auto-generated constructor stub
	}

	public static final String TAG = ESA_003Fg.class.getName();

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		containerWidth = container.getWidth();
		return inflater.inflate(R.layout.esa_003_layout_bak, container , false);
	}

	public interface OnSendActionToServiceListener {
		void sendActionToServer(String action);
	}

	private OnSendActionToServiceListener mListener;

	public void setOnSendActionToServerClick(OnSendActionToServiceListener mListener) {

		if(mListener != null) {
			this.mListener = mListener;
		}
	}
	/*
	@SuppressWarnings("unchecked")
	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {

		initView(view);

		Bundle b = getArguments();
			if(b != null) {
				HashMap<String,String> h = (HashMap<String,String>) b.getSerializable("result");
				ArrayList<String> keys = b.getStringArrayList("keys");
				setViewVisiable(h ,keys);
		}
	}*/

	private View.OnClickListener onClickListener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			if(mListener != null) {
				mListener.sendActionToServer( v.getTag().toString() );
			}
		}
	};

	public void setViewVisiable(HashMap<String,String> mDirs ,  ArrayList<String> keys) {
		//设置中间按钮内容
		tv05.setText(getActivity().getSharedPreferences("center", Context.MODE_PRIVATE).getString("center", null));
		tv05.setVisibility(View.VISIBLE);

		Log.i("ESA003消息:-------------", " "+containerWidth);

		for (int i = 0; i < keys.size(); i++) {

			if(keys.get(i).equals("northwest")) {
				tv01.setVisibility(View.VISIBLE);
				tv01.setText(mDirs.get(keys.get(i)));
				tv01.setTag(keys.get(i));
				tv01.setOnClickListener(onClickListener);
			}/*
			else if(keys.get(i).equals("halt")) {//特殊按钮
				tv01.setVisibility(View.VISIBLE);
				tv01.setText(mDirs.get(keys.get(i)));
				tv01.setTag(keys.get(i));
				tv01.setOnClickListener(onClickListener);
			}*/
			else if(keys.get(i).equals("north")) {
				tv02.setVisibility(View.VISIBLE);
				tv02.setText(mDirs.get(keys.get(i)));
				tv02.setTag(keys.get(i));
				tv02.setOnClickListener(onClickListener);

			}
			else if(keys.get(i).equals("northeast")) {
				tv03.setVisibility(View.VISIBLE);
				tv03.setText(mDirs.get(keys.get(i)));
				tv03.setTag(keys.get(i));
				tv03.setOnClickListener(onClickListener);
			}
			else if(keys.get(i).equals("west")) {
				tv04.setVisibility(View.VISIBLE);
				tv04.setTag(keys.get(i));
				tv04.setText(mDirs.get(keys.get(i)));
				tv04.setOnClickListener(onClickListener);
			}
			else if(keys.get(i).equals("east")) {
				tv06.setVisibility(View.VISIBLE);
				tv06.setText(mDirs.get(keys.get(i)));
				tv06.setTag(keys.get(i));
				tv06.setOnClickListener(onClickListener);
			}
			else if(keys.get(i).equals("southwest")) {
				tv07.setVisibility(View.VISIBLE);
				tv07.setText(mDirs.get(keys.get(i)));
				tv07.setTag(keys.get(i));
				tv07.setOnClickListener(onClickListener);
			}
			else if( keys.get(i).equals("south")) {
				tv08.setVisibility(View.VISIBLE);
				tv08.setText(mDirs.get(keys.get(i)));
				tv08.setTag(keys.get(i));
				tv08.setOnClickListener(onClickListener);
			}
			else if(keys.get(i).equals("southeast")) {
				tv09.setVisibility(View.VISIBLE);
				tv09.setText(mDirs.get(keys.get(i)));
				tv09.setTag(keys.get(i));
				tv09.setOnClickListener(onClickListener);
			}

			//-------------------------------------------

				else if (keys.get(i).equals("northup")){
					tv02.setVisibility(View.VISIBLE);
					tv02.setText(mDirs.get(keys.get(i))+"↑");
					tv02.setTag(keys.get(i));
					tv02.setOnClickListener(onClickListener);

				//northdown 表示 北方 忘下走，其实还是占北方位置
			}else if(keys.get(i).equals("northdown")){

				tv02.setVisibility(View.VISIBLE);
				tv02.setText(mDirs.get(keys.get(i))+"↓");
				tv02.setTag(keys.get(i));
				tv02.setOnClickListener(onClickListener);

			}else if(keys.get(i).equals("southup")){
				tv08.setVisibility(View.VISIBLE);
				tv08.setText(mDirs.get(keys.get(i))+"↑");
				tv08.setTag(keys.get(i));
				tv08.setOnClickListener(onClickListener);
			}else if(keys.get(i).equals("southdown")){
				tv08.setVisibility(View.VISIBLE);
				tv08.setText(mDirs.get(keys.get(i))+"↓");
				tv08.setTag(keys.get(i));
				tv08.setOnClickListener(onClickListener);
			}else if(keys.get(i).equals("eastup")){
				tv06.setVisibility(View.VISIBLE);
				tv06.setText(mDirs.get(keys.get(i))+"↑");
				tv06.setTag(keys.get(i));
				tv06.setOnClickListener(onClickListener);
			}else if(keys.get(i).equals("eastdown")){
				tv06.setVisibility(View.VISIBLE);
				tv06.setText(mDirs.get(keys.get(i))+"↓");
				tv06.setTag(keys.get(i));
				tv06.setOnClickListener(onClickListener);
			}else if(keys.get(i).equals("westdown")){
				tv04.setVisibility(View.VISIBLE);
				tv04.setText(mDirs.get(keys.get(i))+"↓");
				tv04.setTag(keys.get(i));
				tv04.setOnClickListener(onClickListener);
			}else if(keys.get(i).equals("westup")){
				tv04.setVisibility(View.VISIBLE);
				tv04.setText(mDirs.get(keys.get(i))+"↑");
				tv04.setTag(keys.get(i));
				tv04.setOnClickListener(onClickListener);
			}else if(keys.get(i).equals("northeastup")){
				tv03.setVisibility(View.VISIBLE);
				tv03.setText(mDirs.get(keys.get(i))+"↑");
				tv03.setTag(keys.get(i));
				tv03.setOnClickListener(onClickListener);
			}else if(keys.get(i).equals("northeastdown")){
				tv03.setVisibility(View.VISIBLE);
				tv03.setText(mDirs.get(keys.get(i))+"↓");
				tv03.setTag(keys.get(i));
				tv03.setOnClickListener(onClickListener);
			}else if(keys.get(i).equals("southeastdown")){
				tv09.setVisibility(View.VISIBLE);
				tv09.setText(mDirs.get(keys.get(i))+"↓");
				tv09.setTag(keys.get(i));
				tv09.setOnClickListener(onClickListener);
			}else if(keys.get(i).equals("southeastup")){
				tv09.setVisibility(View.VISIBLE);
				tv09.setText(mDirs.get(keys.get(i))+"↑");
				tv09.setTag(keys.get(i));
				tv09.setOnClickListener(onClickListener);
			}

		}

		tv01.getLayoutParams().width = containerWidth / 3;
		tv01.getLayoutParams().height =FUti.dip2px(getActivity(),30);

		tv02.getLayoutParams().width = containerWidth / 3;
		tv02.getLayoutParams().height =FUti.dip2px(getActivity(),30);

		tv03.getLayoutParams().width = containerWidth / 3;
		tv03.getLayoutParams().height =FUti.dip2px(getActivity(),30);

		tv04.getLayoutParams().width = containerWidth / 3;
		tv04.getLayoutParams().height =FUti.dip2px(getActivity(),30);

		tv05.getLayoutParams().width = containerWidth / 3;
		tv05.getLayoutParams().height =FUti.dip2px(getActivity(),30);

		tv06.getLayoutParams().width = containerWidth / 3;
		tv06.getLayoutParams().height =FUti.dip2px(getActivity(),30);

		tv07.getLayoutParams().width = containerWidth / 3;
		tv07.getLayoutParams().height =FUti.dip2px(getActivity(),30);

		tv08.getLayoutParams().width = containerWidth / 3;
		tv08.getLayoutParams().height =FUti.dip2px(getActivity(),30);

		tv09.getLayoutParams().width = containerWidth / 3;
		tv09.getLayoutParams().height =FUti.dip2px(getActivity(),30);




	}

	private TextView tv01;
	private TextView tv02;
	private TextView tv03;
	private TextView tv04;
	private TextView tv05;
	private TextView tv06;
	private TextView tv07;
	private TextView tv08;
	private TextView tv09;

	private void initView(View view) {
		// TODO Auto-generated method stub



		tv01 = (TextView) view.findViewById(R.id.ESA003_01);
		tv02 = (TextView) view.findViewById(R.id.ESA003_02);
		tv03 = (TextView) view.findViewById(R.id.ESA003_03);
		tv04 = (TextView) view.findViewById(R.id.ESA003_04);
		tv05 = (TextView) view.findViewById(R.id.ESA003_05);
		tv06 = (TextView) view.findViewById(R.id.ESA003_06);
		tv07 = (TextView) view.findViewById(R.id.ESA003_07);
		tv08 = (TextView) view.findViewById(R.id.ESA003_08);
		tv09 = (TextView) view.findViewById(R.id.ESA003_09);


		tv01.setTextColor(Color.parseColor(Constant.getAppColor().getFontColor()));
		tv02.setTextColor(Color.parseColor(Constant.getAppColor().getFontColor()));
		tv03.setTextColor(Color.parseColor(Constant.getAppColor().getFontColor()));
		tv04.setTextColor(Color.parseColor(Constant.getAppColor().getFontColor()));
		tv05.setTextColor(Color.parseColor(Constant.getAppColor().getFontColor()));
		tv06.setTextColor(Color.parseColor(Constant.getAppColor().getFontColor()));
		tv07.setTextColor(Color.parseColor(Constant.getAppColor().getFontColor()));
		tv08.setTextColor(Color.parseColor(Constant.getAppColor().getFontColor()));
		tv09.setTextColor(Color.parseColor(Constant.getAppColor().getFontColor()));





	}

	public void onBack() {

		tv01.setVisibility(View.INVISIBLE);
		tv01.setText("");
		tv02.setVisibility(View.INVISIBLE);
		tv02.setText("");
		tv03.setVisibility(View.INVISIBLE);
		tv03.setText("");
		tv04.setVisibility(View.INVISIBLE);
		tv04.setText("");
		tv05.setVisibility(View.INVISIBLE);
		tv05.setText("");
		tv06.setVisibility(View.INVISIBLE);
		tv06.setText("");
		tv07.setVisibility(View.INVISIBLE);
		tv07.setText("");
		tv08.setVisibility(View.INVISIBLE);
		tv08.setText("");
		tv09.setVisibility(View.INVISIBLE);
		tv09.setText("");
	}
}
